package com.lzy.fabric.events;

import com.lzy.fabric.IdlFramework;
import com.lzy.fabric.enchantments.ModEnchantmentInit;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

/**
 * @author 引雷事件
 */
@Mod.EventBusSubscriber(modid = IdlFramework.MODID)
public class ModCiteThunder  {
    /**
     * 引雷事件
     * @param event
     */
    @SubscribeEvent
    public static void onCiteThunder(LivingHurtEvent event){
        //定义受伤者
        EntityLivingBase wounded =  event.getEntityLiving();
        //获取受伤者世界
        World world = wounded.world;
        //判断事件是否被取消-判断是否是客户端
        if (event.isCanceled()||world.isRemote){
            return;
        }
        //定义攻击者
        Entity attacker = event.getSource().getTrueSource();
        //判断攻击者是否是一个生物
        if (attacker instanceof EntityLivingBase){
            EntityLivingBase livingAttacker = (EntityLivingBase) attacker;
            if (ModEnchantmentInit.THUNDER.getLevelOnCreature(livingAttacker) > 0) {
                //添加概率事件
                if (((EntityLivingBase) attacker).getRNG().nextFloat()<0.1f) {
                    world.addWeatherEffect(new EntityLightningBolt(world, wounded.posX, wounded.posY, wounded.posZ, false));
                }
            }
        }
    }
}
